/*
 * THE SOURCE CODES OF THE SOFTWARE ARE VALUABLE ASSETS OF FIDUS TECHNOLOGIES LTD.
 * YOU AGREE TO KEEP ALL SOURCE CODES OF THE SOFTWARE IN CONFIDENCE. YOU MAY NOT TRANSFER
 * OR ASSIGN THE SOFTWARE OR YOUR RIGHTS UNDER THIS AGREEMENT.
 * LIMITED WARRANTY
 * THE SOFTWARE AND ACCOMPANYING WRITTEN MATERIALS (INCLUDING INSTRUCTIONS FOR USE) ARE PROVIDED "AS IS"
 * AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION)HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. YOU ASSUME THE ENTIRE
 * RISK AS TO THE RESULTS AND PERFORMANCE OF THE SOFTWARE.
 * FIDUS TECHNOLOGIES DOES NOT WARRANT THAT THE SOFTWARE WILL MEET ALL YOUR REQUIREMENTS, THAT OPERATIONS
 * OF THE SOFTWARE WILL BE UNINTERRUPTED OR ERROR-FREE, OR THAT ALL SOFTWARE ERRORS WILL BE CORRECTED.
 * FIDUS TECHNOLOGIES-SOFTWARE SOLUTIONS IS NOT RESPONSIBLE FOR PROBLEMS CAUSED BY CHANGES IN THE
 * OPERATING CHARACTERISTICS OF COMPUTER HARDWARE OR COMPUTER OPERATING SYSTEMS WHICH ARE MADE AFTER
 * WHICH ARE MADE AFTER THE RELEASE OF THE SOFTWARE NOR FOR PROBLEMS IN THE INTERACTION OF THE SOFTWARE
 * WITH NON-FIDUS TECHNOLOGIES APPLICATION.WILL HAVE NO RESPONSIBILITY TO REPLACE OR REFUND THE LICENSE FEES
 * FOR MEDIA DAMAGED BY ACCIDENT, ABUSE OR MISAPPLICATION. THIS SOFTWARE IS INTENDED FOR COMMERCIAL,
 * INSTITUTIONAL, AND INDUSTRIAL USERS. THE ABOVE WARRANTIES ARE EXCLUSIVE AND IN LIEU OF ALL OTHER WARRANTIES,
 * WHETHER EXPRESSED OR IMPLIED, INCLUDING THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 * PURPOSE AND NON-INFRINGEMENT. NO ORAL OR WRITTEN INFORMATION OR ADVICE GIVEN BY FIDUS TECHNOLOGIES,
 * ITS EMPLOYEES, DISTRIBUTORS, DEALERS OR AGENTS SHALL INCREASE THE SCOPE OF THE ABOVE WARRANTIES OR
 * CREATE ANY NEW WARRANTIES. NEITHER FIDUS TECHNOLOGIES NOR ANYONE ELSE WHO HAS BEEN INVOLVED IN THE
 * CREATION, PRODUCTION OR DELIVERY OF THE SOFTWARE SHALL BE LIABLE FOR ANY DIRECT, INDIRECT, CONSEQUENTIAL
 * OR INCIDENTAL DAMAGES (INCLUDING DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS LOSSES, BUSINESS
 * INTERRUPTION, LOSS OF DATA, AND THE LIKE) ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE,
 * OR ANY OTHER CLAIM BY ANY PARTY EVEN IF FIDUS TECHNOLOGIES HAS BEEN ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGES. IN THE EVENT FIDUS TECHNOLOGIES IS LIABLE FOR ANY DAMAGES, FIDUS TECHNOLOGIES' LIABILITY
 * FOR DAMAGES SHALL BE LIMITED TO THE AMOUNT PAID FOR THE SOFTWARE UNDER THE TERMS AND CONDITIONS OF THIS
 * AGREEMENT.
 * http://www.fidustechnologies.com
 * Copyright(C) 2010 FIDUS TECHNOLOGIES-Software Solutions(R)
 * All rights reserved.
 */

/*
 * CustomerDetailsView.java
 *
 * Created on 7 Sep, 2010, 2:40:26 PM
 */

package com.fidus.inventory.UI.customer;

import com.fidus.inventory.beans.customer.CustomerDetails;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import javax.persistence.RollbackException;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.jdesktop.application.Action;
import org.jdesktop.beansbinding.AbstractBindingListener;
import org.jdesktop.beansbinding.Binding;
import org.jdesktop.beansbinding.PropertyStateEvent;

/**
 *
 * @author Mahesh Kedari<mahesh.kedari@fidustechnologies.com>
 */
public class CustomerDetailsView extends javax.swing.JPanel {

    /** Creates new form CustomerDetailsView */
    public CustomerDetailsView() {
        initComponents();
        entityManager.getTransaction().begin();
        bindingGroup.addBindingListener(new AbstractBindingListener() {
            @Override
            public void targetChanged(Binding binding, PropertyStateEvent event) {
                setSaveNeeded(true);
            }
        });
        addressTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent lse) {
                propertyChangeSupport.firePropertyChange(PROP_ADDRESSSELECTED, !isAddressSelected(), isAddressSelected());
            }
        });
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        entityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("inventorymanagement_dbPU").createEntityManager();
        customerAddressDetailsQuery = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT c FROM CustomerAddressDetails c");
        customerAddressDetailsList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(customerAddressDetailsQuery.getResultList());
        contactPersonDetailsCustQuery = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT c FROM ContactPersonDetailsCust c");
        contactPersonDetailsCustList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(contactPersonDetailsCustQuery.getResultList());
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(simple_inventory.Simple_InventoryApp.class).getContext().getResourceMap(CustomerDetailsView.class);
        customerDetailsQuery = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery(resourceMap.getString("customerDetailsQuery.query")); // NOI18N
        customerDetailsList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : customerDetailsQuery.getResultList();
        custLogoPopupMenu = new javax.swing.JPopupMenu();
        browseLogoMenuItem = new javax.swing.JMenuItem();
        removeLogoMenuItem = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JPopupMenu.Separator();
        viewLogoMenuItem = new javax.swing.JMenuItem();
        contactPersonPopupMenu = new javax.swing.JPopupMenu();
        viewContactMenuItem = new javax.swing.JMenuItem();
        jSeparator3 = new javax.swing.JPopupMenu.Separator();
        addContactMenuItem = new javax.swing.JMenuItem();
        deleteContactMenuItem = new javax.swing.JMenuItem();
        jSeparator4 = new javax.swing.JPopupMenu.Separator();
        printContactMenuItem = new javax.swing.JMenuItem();
        custAddressPopupMenu = new javax.swing.JPopupMenu();
        viewAddressMenuItem = new javax.swing.JMenuItem();
        jSeparator5 = new javax.swing.JPopupMenu.Separator();
        addAddressMenuItem = new javax.swing.JMenuItem();
        deleteAddressMenuItem = new javax.swing.JMenuItem();
        jSeparator6 = new javax.swing.JPopupMenu.Separator();
        printAddressMenuItem = new javax.swing.JMenuItem();
        titleLabel = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        toolBar = new javax.swing.JToolBar();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        customerNameLabel = new javax.swing.JLabel();
        customerNameTextField = new javax.swing.JTextField();
        descriptionLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        descriptionTextArea = new javax.swing.JTextArea();
        addressLabel = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        addressTable = new org.jdesktop.swingx.JXTable();
        customerLogoLabel = new javax.swing.JLabel();
        customerLogoPanel = new org.jdesktop.swingx.JXImagePanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        contactPersonTable = new org.jdesktop.swingx.JXTable();

        custLogoPopupMenu.setName("custLogoPopupMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(simple_inventory.Simple_InventoryApp.class).getContext().getActionMap(CustomerDetailsView.class, this);
        browseLogoMenuItem.setAction(actionMap.get("browseLogo")); // NOI18N
        browseLogoMenuItem.setText(resourceMap.getString("browseLogoMenuItem.text")); // NOI18N
        browseLogoMenuItem.setName("browseLogoMenuItem"); // NOI18N
        custLogoPopupMenu.add(browseLogoMenuItem);

        removeLogoMenuItem.setAction(actionMap.get("removeLogo")); // NOI18N
        removeLogoMenuItem.setText(resourceMap.getString("removeLogoMenuItem.text")); // NOI18N
        removeLogoMenuItem.setName("removeLogoMenuItem"); // NOI18N
        custLogoPopupMenu.add(removeLogoMenuItem);

        jSeparator2.setName("jSeparator2"); // NOI18N
        custLogoPopupMenu.add(jSeparator2);

        viewLogoMenuItem.setAction(actionMap.get("viewCustomerLogo")); // NOI18N
        viewLogoMenuItem.setText(resourceMap.getString("viewLogoMenuItem.text")); // NOI18N
        viewLogoMenuItem.setName("viewLogoMenuItem"); // NOI18N
        custLogoPopupMenu.add(viewLogoMenuItem);

        contactPersonPopupMenu.setName("contactPersonPopupMenu"); // NOI18N

        viewContactMenuItem.setAction(actionMap.get("viewSelectedContact")); // NOI18N
        viewContactMenuItem.setText(resourceMap.getString("viewContactMenuItem.text")); // NOI18N
        viewContactMenuItem.setActionCommand(resourceMap.getString("viewContactMenuItem.actionCommand")); // NOI18N
        viewContactMenuItem.setName("viewContactMenuItem"); // NOI18N
        contactPersonPopupMenu.add(viewContactMenuItem);

        jSeparator3.setName("jSeparator3"); // NOI18N
        contactPersonPopupMenu.add(jSeparator3);

        addContactMenuItem.setAction(actionMap.get("addContactPerson")); // NOI18N
        addContactMenuItem.setText(resourceMap.getString("addContactMenuItem.text")); // NOI18N
        addContactMenuItem.setActionCommand(resourceMap.getString("addContactMenuItem.actionCommand")); // NOI18N
        addContactMenuItem.setName("addContactMenuItem"); // NOI18N
        contactPersonPopupMenu.add(addContactMenuItem);

        deleteContactMenuItem.setAction(actionMap.get("deleteSelectedContact")); // NOI18N
        deleteContactMenuItem.setText(resourceMap.getString("deleteContactMenuItem.text")); // NOI18N
        deleteContactMenuItem.setActionCommand(resourceMap.getString("deleteContactMenuItem.actionCommand")); // NOI18N
        deleteContactMenuItem.setName("deleteContactMenuItem"); // NOI18N
        contactPersonPopupMenu.add(deleteContactMenuItem);

        jSeparator4.setName("jSeparator4"); // NOI18N
        contactPersonPopupMenu.add(jSeparator4);

        printContactMenuItem.setAction(actionMap.get("printSelectedContact")); // NOI18N
        printContactMenuItem.setText(resourceMap.getString("printContactMenuItem.text")); // NOI18N
        printContactMenuItem.setActionCommand(resourceMap.getString("printContactMenuItem.actionCommand")); // NOI18N
        printContactMenuItem.setName("printContactMenuItem"); // NOI18N
        contactPersonPopupMenu.add(printContactMenuItem);

        custAddressPopupMenu.setName("custAddressPopupMenu"); // NOI18N

        viewAddressMenuItem.setAction(actionMap.get("viewCustomerAddress")); // NOI18N
        viewAddressMenuItem.setText(resourceMap.getString("viewAddressMenuItem.text")); // NOI18N
        viewAddressMenuItem.setName("viewAddressMenuItem"); // NOI18N
        custAddressPopupMenu.add(viewAddressMenuItem);

        jSeparator5.setName("jSeparator5"); // NOI18N
        custAddressPopupMenu.add(jSeparator5);

        addAddressMenuItem.setAction(actionMap.get("addNewAddress")); // NOI18N
        addAddressMenuItem.setText(resourceMap.getString("addAddressMenuItem.text")); // NOI18N
        addAddressMenuItem.setName("addAddressMenuItem"); // NOI18N
        custAddressPopupMenu.add(addAddressMenuItem);

        deleteAddressMenuItem.setAction(actionMap.get("removeAddress")); // NOI18N
        deleteAddressMenuItem.setText(resourceMap.getString("deleteAddressMenuItem.text")); // NOI18N
        deleteAddressMenuItem.setName("deleteAddressMenuItem"); // NOI18N
        custAddressPopupMenu.add(deleteAddressMenuItem);

        jSeparator6.setName("jSeparator6"); // NOI18N
        custAddressPopupMenu.add(jSeparator6);

        printAddressMenuItem.setAction(actionMap.get("printAddress")); // NOI18N
        printAddressMenuItem.setText(resourceMap.getString("printAddressMenuItem.text")); // NOI18N
        printAddressMenuItem.setActionCommand(resourceMap.getString("printAddressMenuItem.actionCommand")); // NOI18N
        printAddressMenuItem.setName("printAddressMenuItem"); // NOI18N
        custAddressPopupMenu.add(printAddressMenuItem);

        setName("Form"); // NOI18N

        titleLabel.setFont(resourceMap.getFont("titleLabel.font")); // NOI18N
        titleLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        titleLabel.setLabelFor(this);
        titleLabel.setText(resourceMap.getString("titleLabel.text")); // NOI18N
        titleLabel.setName("titleLabel"); // NOI18N

        jSeparator1.setName("jSeparator1"); // NOI18N

        toolBar.setFloatable(false);
        toolBar.setRollover(true);
        toolBar.setName("toolBar"); // NOI18N

        jButton1.setAction(actionMap.get("newCustomer")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setName("jButton1"); // NOI18N
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        toolBar.add(jButton1);

        jButton2.setAction(actionMap.get("saveRecord")); // NOI18N
        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setFocusable(false);
        jButton2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton2.setName("jButton2"); // NOI18N
        jButton2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        toolBar.add(jButton2);

        jButton3.setAction(actionMap.get("resetForm")); // NOI18N
        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setFocusable(false);
        jButton3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton3.setName("jButton3"); // NOI18N
        jButton3.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        toolBar.add(jButton3);

        jButton4.setAction(actionMap.get("deleteRecord")); // NOI18N
        jButton4.setText(resourceMap.getString("jButton4.text")); // NOI18N
        jButton4.setFocusable(false);
        jButton4.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton4.setName("jButton4"); // NOI18N
        jButton4.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        toolBar.add(jButton4);

        jButton5.setAction(actionMap.get("printDetails")); // NOI18N
        jButton5.setText(resourceMap.getString("jButton5.text")); // NOI18N
        jButton5.setFocusable(false);
        jButton5.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton5.setName("jButton5"); // NOI18N
        jButton5.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        toolBar.add(jButton5);

        customerNameLabel.setLabelFor(customerNameTextField);
        customerNameLabel.setText(resourceMap.getString("customerNameLabel.text")); // NOI18N
        customerNameLabel.setName("customerNameLabel"); // NOI18N

        customerNameTextField.setName("customerNameTextField"); // NOI18N

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${selectedCustomer.customerName}"), customerNameTextField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${validCustomer}"), customerNameTextField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        descriptionLabel.setLabelFor(descriptionTextArea);
        descriptionLabel.setText(resourceMap.getString("descriptionLabel.text")); // NOI18N
        descriptionLabel.setName("descriptionLabel"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        descriptionTextArea.setColumns(15);
        descriptionTextArea.setRows(3);
        descriptionTextArea.setWrapStyleWord(true);
        descriptionTextArea.setName("descriptionTextArea"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${selectedCustomer.description}"), descriptionTextArea, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${validCustomer}"), descriptionTextArea, org.jdesktop.beansbinding.BeanProperty.create("editable"));
        bindingGroup.addBinding(binding);

        jScrollPane1.setViewportView(descriptionTextArea);

        addressLabel.setLabelFor(addressTable);
        addressLabel.setText(resourceMap.getString("addressLabel.text")); // NOI18N
        addressLabel.setName("addressLabel"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        addressTable.setColumnSelectionAllowed(true);
        addressTable.setName("addressTable"); // NOI18N

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, customerAddressDetailsList, addressTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${streetLine1}"));
        columnBinding.setColumnName("Street Line1");
        columnBinding.setColumnClass(String.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${validCustomer}"), addressTable, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        addressTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                addressTableMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(addressTable);
        addressTable.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        addressTable.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("addressTable.columnModel.title0")); // NOI18N

        customerLogoLabel.setLabelFor(customerLogoPanel);
        customerLogoLabel.setText(resourceMap.getString("customerLogoLabel.text")); // NOI18N
        customerLogoLabel.setName("customerLogoLabel"); // NOI18N

        customerLogoPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
        customerLogoPanel.setName("customerLogoPanel"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${validCustomer}"), customerLogoPanel, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        customerLogoPanel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                customerLogoPanelMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout customerLogoPanelLayout = new javax.swing.GroupLayout(customerLogoPanel);
        customerLogoPanel.setLayout(customerLogoPanelLayout);
        customerLogoPanelLayout.setHorizontalGroup(
            customerLogoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 115, Short.MAX_VALUE)
        );
        customerLogoPanelLayout.setVerticalGroup(
            customerLogoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 105, Short.MAX_VALUE)
        );

        jScrollPane3.setName("jScrollPane3"); // NOI18N

        contactPersonTable.setColumnSelectionAllowed(true);
        contactPersonTable.setName("contactPersonTable"); // NOI18N

        jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, contactPersonDetailsCustList, contactPersonTable);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${contactFirstName}"));
        columnBinding.setColumnName("Contact First Name");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${contactLastName}"));
        columnBinding.setColumnName("Contact Last Name");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${description}"));
        columnBinding.setColumnName("Description");
        columnBinding.setColumnClass(String.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        contactPersonTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                contactPersonTableMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(contactPersonTable);
        contactPersonTable.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        contactPersonTable.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("contactPersonTable.columnModel.title0")); // NOI18N
        contactPersonTable.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("contactPersonTable.columnModel.title1")); // NOI18N
        contactPersonTable.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("contactPersonTable.columnModel.title2")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(customerNameLabel)
                    .addComponent(descriptionLabel)
                    .addComponent(customerLogoLabel)
                    .addComponent(addressLabel))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(customerNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(27, 27, 27)
                                .addComponent(customerLogoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(20, 20, 20)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)
                .addContainerGap())
            .addComponent(toolBar, javax.swing.GroupLayout.DEFAULT_SIZE, 652, Short.MAX_VALUE)
            .addComponent(titleLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 652, Short.MAX_VALUE)
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 652, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(titleLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(toolBar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(5, 5, 5)
                                .addComponent(customerNameLabel)
                                .addGap(31, 31, 31)
                                .addComponent(descriptionLabel)
                                .addGap(116, 116, 116)
                                .addComponent(customerLogoLabel)
                                .addGap(112, 112, 112)
                                .addComponent(addressLabel))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(customerNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(26, 26, 26)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(27, 27, 27)
                                .addComponent(customerLogoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE))))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)))
                .addContainerGap())
        );

        bindingGroup.bind();
    }// </editor-fold>//GEN-END:initComponents

    private void customerLogoPanelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_customerLogoPanelMouseClicked
        if(evt.getButton()== java.awt.event.MouseEvent.BUTTON3){
            custLogoPopupMenu.show(customerLogoPanel, evt.getX(), evt.getY());
        }
    }//GEN-LAST:event_customerLogoPanelMouseClicked

    private void addressTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_addressTableMouseClicked
        if(evt.getButton()== java.awt.event.MouseEvent.BUTTON3){
            custAddressPopupMenu.show(addressTable, evt.getX(), evt.getY());
        }
    }//GEN-LAST:event_addressTableMouseClicked

    private void contactPersonTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_contactPersonTableMouseClicked
        if(evt.getButton()== java.awt.event.MouseEvent.BUTTON3){
            contactPersonPopupMenu.show(contactPersonTable, evt.getX(), evt.getY());
        }
    }//GEN-LAST:event_contactPersonTableMouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem addAddressMenuItem;
    private javax.swing.JMenuItem addContactMenuItem;
    private javax.swing.JLabel addressLabel;
    private org.jdesktop.swingx.JXTable addressTable;
    private javax.swing.JMenuItem browseLogoMenuItem;
    private java.util.List<com.fidus.inventory.beans.customer.ContactPersonDetailsCust> contactPersonDetailsCustList;
    private javax.persistence.Query contactPersonDetailsCustQuery;
    private javax.swing.JPopupMenu contactPersonPopupMenu;
    private org.jdesktop.swingx.JXTable contactPersonTable;
    private javax.swing.JPopupMenu custAddressPopupMenu;
    private javax.swing.JPopupMenu custLogoPopupMenu;
    private java.util.List<com.fidus.inventory.beans.customer.CustomerAddressDetails> customerAddressDetailsList;
    private javax.persistence.Query customerAddressDetailsQuery;
    private java.util.List<com.fidus.inventory.beans.customer.CustomerDetails> customerDetailsList;
    private javax.persistence.Query customerDetailsQuery;
    private javax.swing.JLabel customerLogoLabel;
    private org.jdesktop.swingx.JXImagePanel customerLogoPanel;
    private javax.swing.JLabel customerNameLabel;
    private javax.swing.JTextField customerNameTextField;
    private javax.swing.JMenuItem deleteAddressMenuItem;
    private javax.swing.JMenuItem deleteContactMenuItem;
    private javax.swing.JLabel descriptionLabel;
    private javax.swing.JTextArea descriptionTextArea;
    private javax.persistence.EntityManager entityManager;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JPopupMenu.Separator jSeparator2;
    private javax.swing.JPopupMenu.Separator jSeparator3;
    private javax.swing.JPopupMenu.Separator jSeparator4;
    private javax.swing.JPopupMenu.Separator jSeparator5;
    private javax.swing.JPopupMenu.Separator jSeparator6;
    private javax.swing.JMenuItem printAddressMenuItem;
    private javax.swing.JMenuItem printContactMenuItem;
    private javax.swing.JMenuItem removeLogoMenuItem;
    private javax.swing.JLabel titleLabel;
    private javax.swing.JToolBar toolBar;
    private javax.swing.JMenuItem viewAddressMenuItem;
    private javax.swing.JMenuItem viewContactMenuItem;
    private javax.swing.JMenuItem viewLogoMenuItem;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    //<editor-fold defaultstate="collapsed" desc="Handle Selected Customer">
    protected CustomerDetails selectedCustomer;
    /**
     * This property contains customer object being processed in this form
     */
    public static final String PROP_SELECTEDCUSTOMER = "selectedCustomer";

    /**
     * Get the value of selectedCustomer
     *
     * @return the value of selectedCustomer
     */
    public CustomerDetails getSelectedCustomer() {
        return selectedCustomer;
    }

    /**
     * Set the value of selectedCustomer
     *
     * @param selectedCustomer new value of selectedCustomer
     */
    public void setSelectedCustomer(CustomerDetails selectedCustomer) {
        CustomerDetails oldCustomerDetails = this.selectedCustomer;
        this.selectedCustomer = selectedCustomer;
        propertyChangeSupport.firePropertyChange(PROP_SELECTEDCUSTOMER, oldCustomerDetails, selectedCustomer);
    }


    protected boolean validCustomer;

    /**
     * This property will show whether the object in selectedCustomer is
     * valid or not
     */
    public static final String PROP_VALIDCUSTOMER = "validCustomer";

    /**
     * Get the value of validCustomer
     *
     * @return true: if selectedCustomer is a valid Object of CustomerDetails <br/>
     *         false: otherwise
     */
    public boolean isValidCustomer() {
        return getSelectedCustomer()!=null;
    }

    /**
     * Set the value of validCustomer
     *
     * @param validCustomer new value of validCustomer
     */
    public void setValidCustomer(boolean validCustomer) {
        boolean oldValidCustomer = this.validCustomer;
        this.validCustomer = validCustomer;
        propertyChangeSupport.firePropertyChange(PROP_VALIDCUSTOMER, oldValidCustomer, validCustomer);
    }
//</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Tool bar Action handling">
    /**
     * This action will handle new customer creation
     */
    @Action
    public void newCustomer(){
        CustomerDetails customerDetails = new CustomerDetails();
        customerDetailsList.add(customerDetails);
        entityManager.persist(customerDetails);
        System.out.println("Valid Customer");
        setSelectedCustomer(customerDetails);
        setValidCustomer(true);
    }


    protected boolean saveNeeded;
    /**
     * This property Enables/Disables save button as per the data change in form
     */
    public static final String PROP_SAVENEEDED = "saveNeeded";

    /**
     * Check if uncommitted changes are present in the form.
     *
     * @return true if uncommitted changes are present in form<br/>
     *         false, otherwise
     */
    public boolean isSaveNeeded() {
        return saveNeeded;
    }
    
    /**
     * Set the value of saveNeeded
     *
     * @param saveNeeded new value of saveNeeded
     */
    public void setSaveNeeded(boolean saveNeeded) {
        boolean oldSaveNeeded = this.saveNeeded;
        this.saveNeeded = saveNeeded;
        propertyChangeSupport.firePropertyChange(PROP_SAVENEEDED, oldSaveNeeded, saveNeeded);
    }

    /**
     * Save uncommitted changes in the form. This button is enabled only if
     * the value of saveNeeded is true;
     */
        @Action(enabledProperty="saveNeeded")
    public void saveRecord(){
        try {
            entityManager.getTransaction().commit();
            JOptionPane.showMessageDialog(null, "Customer Saved Successfully", "Customer Details", JOptionPane.PLAIN_MESSAGE);
            setSaveNeeded(true);
        } catch (RollbackException ex) {
            //TODO handle Rollback Exception ex
        } finally {
            // If transaction is not active, start new transaction
            if(!entityManager.getTransaction().isActive()){
                entityManager.getTransaction().begin();
            }
        }
    }

    /**
     * This action will Delete current customer from the database
     */
        @Action(enabledProperty="validCustomer")
    public void deleteRecord(){
        // Ask user for confirmation
        int choice = JOptionPane.showConfirmDialog(null, "Are you sure you want to delete this customer?",
                "Customer Details", JOptionPane.YES_NO_OPTION);
        // If user choice is NO, return from this function
        if(choice==JOptionPane.NO_OPTION){
            return;
        }
        // If user choice is YES, perform below operation
        customerDetailsList.remove(getSelectedCustomer());
        entityManager.remove(getSelectedCustomer());
        setSelectedCustomer(null);
        setValidCustomer(false);
    }

    /**
     * This action will reset the form
     */
    @Action
    public void resetForm(){
        //TODO reset customer details form
    }

    /**
     * This action will print details about current customer
     */
    @Action
    public void printDetails(){
        //TODO print customer details
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Handle Customer Logo Popup">
    /**
     * This action will handle adding new logo to customer
     */
    @Action
    public void browseLogo(){
        //TODO Handle browse customer logo
    }

    /**
     * This action will handle removing customer logo
     */
    @Action
    public void removeLogo(){
        //TODO Handle remove Customer Logo
    }

    /**
     * This action will show the customer logo in default image viewer of OS
     */
    @Action
    public void viewCustomerLogo(){
        //TODO View Customer Logo
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Handle Customer Contact Person Popup">
    /**
     * This action will handle Adding new contact person to current customer
     */
    @Action
    public void addContactPerson(){
        //TODO handle adding new contact person
    }


    protected boolean contactSelected;
    public static final String PROP_CONTACTSELECTED = "contactSelected";

    /**
     * Get the value of contactSelected
     *
     * @return true: if any one of the contact person is selected<br/>
     *         false: otherwise
     */
    public boolean isContactSelected() {
        return contactPersonTable.getSelectedRow()!=-1;
    }

    /**
     * Set the value of contactSelected
     *
     * @param contactSelected new value of contactSelected
     */
    public void setContactSelected(boolean contactSelected) {
        boolean oldContactSelected = this.contactSelected;
        this.contactSelected = contactSelected;
        propertyChangeSupport.firePropertyChange(PROP_CONTACTSELECTED, oldContactSelected, contactSelected);
    }
    /**
     * This action will delete selected contact person of current customer
     */
        @Action(enabledProperty="contactSelected")
    public void deleteSelectedContact(){
        //TODO Delete selected customer contact person
    }

    /**
     * This action will view details of selected contact person
     */
        @Action(enabledProperty="contactSelected")
    public void viewSelectedContact(){
        //TODO view details of selected contact person
    }

    /**
     * This action will print details of selected contact person
     */
        @Action(enabledProperty="contactSelected")
    public void printSelectedContact(){
        //TODO print details of selected contact person
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Handle Customer Address popup">
   
    protected boolean addressSelected;
    /**
     * This property will show if any record in address table is selected
     */
    public static final String PROP_ADDRESSSELECTED = "addressSelected";
    
    /**
     * Get if any record in address table is selected
     *
     * @return true: If any of the record in customer address details is selected<br/>
     *         false: otherwise
     * 
     */
    public boolean isAddressSelected() {
        return addressTable.getSelectedRow()!=-1;
    }

    /**
     * Set the value of addressSelected
     *
     * @param addressSelected new value of addressSelected
     */
    public void setAddressSelected(boolean addressSelected) {
        boolean oldAddressSelected = this.addressSelected;
        this.addressSelected = addressSelected;
        propertyChangeSupport.firePropertyChange(PROP_ADDRESSSELECTED, oldAddressSelected, addressSelected);
    }
    
    /**
     * This action will display details of selected address of current customer
     */
        @Action(enabledProperty="addressSelected")
    public void viewCustomerAddress(){
        //TODO view Customer Addrsss
    }

    /**
     * This action will add new address to current customer
     */
    @Action
    public void addNewAddress(){
        //TODO add new address for selected customer
    }

    /**
     * This action will remove selected address of current customer
     */
        @Action(enabledProperty="addressSelected")
    public void removeAddress(){
        //TODO remove selected address of customer
    }

    /**
     * This action will print details of selected address of current customer
     */
        @Action(enabledProperty="addressSelected")
    public void printAddress(){
        //TODO print selected address of current customer
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Property Change Support">
    private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);

    /**
     * Add PropertyChangeListener.
     *
     * @param listener
     */
    @Override
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.addPropertyChangeListener(listener);
    }

    /**
     * Remove PropertyChangeListener.
     *
     * @param listener
     */
    @Override
    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.removePropertyChangeListener(listener);
    }
    //</editor-fold>

}
